package xyz.sicau.blog.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.sicau.blog.bean.Blog;
import xyz.sicau.blog.dao.BlogMapper;
import xyz.sicau.blog.dao.UserMapper;
import xyz.sicau.blog.utils.DiyMethod;
import xyz.sicau.blog.utils.JwtUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "博客接口")
@RestController
public class BlogController {
    @Resource
    BlogMapper blogMapper;
    @Resource
    UserMapper userMapper;

    @Resource
    DiyMethod diyMethod;

    // 返回博客列表
    @RequestMapping("/blogs")
    public List<Blog> doBlogs(int start, int page) {
//        System.out.println("===blogs请求运行==============================");
//        System.out.println("start=" + start + ", page=" + page);
        return blogMapper.getBlogPage(start, page);
    }

    // 上传博客，并返回结果
    // blogTittle,blogContent,userToken,blogTime,blogLabel,blogImg,blogData,blogType
    @RequestMapping("/release")
    public int releaseBlog(String userToken, String blogJson) {
        System.out.println("===release请求运行==============================");
        int userId = JwtUtils.getUserIdByToken(userToken);
        String userName = userMapper.getUserNameById(userId);
        if (userId <= 0) return 0;

        Blog blog = JSON.parseObject(blogJson, Blog.class);
        blog.setBlogUserid(String.valueOf(userId));
        blog.setBlogUsername(userName);
        // 数据库不会给null赋一个默认值，需要自己添加，不然日期是null
        if (blog.getBlogTime() == null) {
            blog.setBlogTime(new Date());
        }

        // 检查博客
        if (diyMethod.checkBlog(blog)) {
            return blogMapper.insertBlog(blog);
        }
        return 0;
    }

    // 爬虫专用Service，用的是普通的用户名和id
    @RequestMapping("/release_pc")
    public int releaseBlogPC(String blogJson) {
        System.out.println("===release请求运行==============================");
        System.out.println(blogJson);
        Blog blog = JSON.parseObject(blogJson, Blog.class);

        blog.setBlogUsername(userMapper.getUserNameById(Integer.parseInt(blog.getBlogUserid())));
        if (blog.getBlogTime() == null) {
            blog.setBlogTime(new Date());
        }
        // System.out.println(blog);

        // 在数据库中检查
        if (blog.getBlogType().equals("pc")) {
            List<Blog> blogs = blogMapper.getBlogsByUsername(blog.getBlogUsername());
            // 找到此用户前后一分钟发的blog
            for (Blog iblog : blogs) {
                long time = blog.getBlogTime().getTime();
                if (iblog.getBlogTime().getTime() > time - 60000 && iblog.getBlogTime().getTime() < time + 60000) {
                    System.out.println("checkBlog：blog已存在，博客发送失败");
                    return 0;
                }
            }
        }
        return blogMapper.insertBlog(blog);
    }

    // 返回各标签博客的统计数据
    @RequestMapping("/count")
    public String doCount() {
        List<Map<String, Integer>> blogCount = blogMapper.getBlogCount();
        return JSON.toJSONString(blogCount);
    }

    // 通过博客ID查询单条博客
    @RequestMapping("/blog")
    public Blog blogById(@RequestParam("id") int blogId) {
        return blogMapper.getBlogById(blogId);
    }

    // 通过用户名查询博客s，调blogsByUserId
    @RequestMapping("/blogs_by_username")
    public List<Blog> blogsByUserName(String username, int start, int page) {
        int userId = userMapper.getUserIdByName(username);
        return blogsByUserId(userId, start, page);
    }

    // 通过用户id查询博客s
    @RequestMapping("/blogs_by_userid")
    public List<Blog> blogsByUserId(int userid, int start, int page) {
        return blogMapper.getBlogsPageByUserid(userid, start, page);
    }

}
